﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web;

namespace Common
{
    /// <summary>
    /// HttpRequest操作助手
    /// </summary>
    public static class Helper_HttpPostGet
    {
        static Encoding getEncoding = Encoding.UTF8;

        public static string DoPost(string strUrl, string strParm,Encoding encodes)
        {
            Encoding encode = encodes;
            byte[] arrB = encode.GetBytes(strParm);
            string strBaseUrl = null;
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl);
            myReq.Method = "POST";
            myReq.ContentType = "multipart/form-data";
            myReq.ContentLength = arrB.Length;
            Stream outStream = myReq.GetRequestStream();
            outStream.Write(arrB, 0, arrB.Length);
            outStream.Close();
            WebResponse myResp = null;
            try
            {
                //接收HTTP做出的响应
                myResp = myReq.GetResponse();
            }
            catch (Exception e)
            {
                int ii = 0;
            }
            Stream ReceiveStream = myResp.GetResponseStream();
            StreamReader readStream = new StreamReader(ReceiveStream, encode);
            Char[] read = new Char[256];
            int count = readStream.Read(read, 0, 256);
            string str = null;
            while (count > 0)
            {
                str += new String(read, 0, count);
                count = readStream.Read(read, 0, 256);
            }
            readStream.Close();
            myResp.Close();
            return str;
        }

        /// <summary>
        /// 执行一个GET请求，并返回Response字符串
        /// </summary>
        /// <param name="strUrl"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string DoGet(string strUrl, Encoding encoding)
        {
            string strRet = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
                request.Timeout = 2000;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                System.IO.Stream resStream = response.GetResponseStream();
                StreamReader readStream = new StreamReader(resStream, encoding);
                Char[] read = new Char[256];
                int count = readStream.Read(read, 0, 256);
                while (count > 0)
                {
                    String str = new String(read, 0, count);
                    strRet = strRet + str;
                    count = readStream.Read(read, 0, 256);
                }
                resStream.Close();
            }
            catch (Exception e)
            {
                strRet = "";
            }
            return strRet;
        }
        /// <summary>
        /// 执行一个GET请求，并返回Response字符串
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        public static string DoGet(string strUrl)
        {
            string strRet = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
                request.Timeout = 2000;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                System.IO.Stream resStream = response.GetResponseStream();
                StreamReader readStream = new StreamReader(resStream);
                Char[] read = new Char[256];
                int count = readStream.Read(read, 0, 256);
                while (count > 0)
                {
                    String str = new String(read, 0, count);
                    strRet = strRet + str;
                    count = readStream.Read(read, 0, 256);
                }
                resStream.Close();
            }
            catch (Exception e)
            {
                strRet = "";
            }
            return strRet;
        }

        public static string PostTo(string dataStr, string _url)
        {
            return PostTo(dataStr, _url, "multipart/form-data", "UTF-8");
        }
        public static string PostTo(string dataStr, string _url, string contentType,string encoding)
        {
            try
            {
                //Request = "timerNum=" + Request;
                System.Uri destination = new System.Uri(_url);
                byte[] requestBytes = Encoding.GetEncoding(encoding).GetBytes(dataStr);
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(destination);
                webRequest.ContentType = contentType;
                webRequest.Method = "POST";
                webRequest.ContentLength = requestBytes.Length;

                Stream reqStream = webRequest.GetRequestStream();
                reqStream.Write(requestBytes, 0, requestBytes.Length);
                reqStream.Close();
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                if (webRequest.HaveResponse)
                {
                    StreamReader stream = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
                    string responseString = stream.ReadToEnd();
                    stream.Close();

                    webResponse.Close();
                    return responseString;
                }
                throw new ApplicationException("[主机响应出错]");
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }
    }
}
